In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the program start. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is similar to a buffer overflow), the stack is said to overflow, typically resulting in a program crash.
Here is an example of infinite recursion in C:
return foo();
}
The function foo, when it is invoked, continues to invoke itself, allocating additional stack space each time, until the stack overflows, resulting in a segmentation fault. However, some compilers implement tail-call optimization, allowing infinite recursion of a specific sort—tail recursion—to occur without stack overflow. This works because tail-recursion calls do not take up additional stack space.
Some C compiler options will effectively enable tail-call optimization; for example, compiling the above simple program using gcc with -O1 will result in a segmentation fault, but not when using -O2 or -O3, since these optimization levels imply the -foptimize-sibling-calls compiler option. Other languages, such as Scheme, require all implementations to include tail-recursion as part of the language standard.
SomeObject* next;
};
void someFunction(SomeObject* argument) { if (getCondition()) {
someFunction(argument.next);
}
}
| s.push(argument); while (!s.empty()) { argument = s.pop();
if (condition) {
s.push(argument.next);
}
}
|
A primitive recursive function like the one on the left side can always be transformed into a loop like on the right side.
A function like the example above on the left would not be a problem in an environment supporting tail-call optimization; however, it is still possible to create a recursive function that may result in a stack overflow in these languages. Consider the example below of two simple integer exponentiation functions.
if (exp > 0) {
return base * pow(base, exp - 1);
} else {
return 1;
}
}
| return pow_accum(base, exp, 1);
}
int pow_accum(int base, int exp, int accum) { if (exp > 0) {
return pow_accum(base, exp - 1, accum * base);
} else {
return accum;
}
}
|
Both pow(base, exp) functions above compute an equivalent result, however, the one on the left is prone to causing a stack overflow because tail-call optimization is not possible for this function. During execution, the stack for these functions will look like this:
Notice that the function on the left must store in its stack exp number of integers, which will be multiplied when the recursion terminates and the function returns 1. In contrast, the function at the right must only store 3 integers at any time, and computes an intermediary result which is passed to its following invocation. As no other information outside of the current function invocation must be stored, a tail-recursion optimizer can "drop" the prior stack frames, eliminating the possibility of a stack overflow.
An example of a very large stack variable in C:
double x[1048576] = {0};
}
On a C implementation with 8 byte double-precision floats, the declared array consumes 8 megabytes of data; if this is more memory than is available on the stack (as set by thread creation parameters or operating system limits), a stack overflow will occur.
|
|